import { NextApiRequest, NextApiResponse } from 'next';
import { withIronSessionApiRoute } from 'iron-session/next';
import { Cookie } from 'next-cookie';

import { ISession } from '@/pages/api';
import { ironOptions } from '@/config';
import { clearCookie } from '@/utils';

const logOut = async (req: NextApiRequest, res: NextApiResponse) => {
  const session: ISession = req.session;
  const cookies = Cookie.fromApiRoute(req, res);

  // 清除 session 的信息
  await session.destroy();
  clearCookie(cookies);

  res?.status(200).json({
    code: 200,
    message: '退出登录成功',
    data: {},
  });
};

export default withIronSessionApiRoute(logOut, ironOptions);
